package cn.iocoder.yudao.module.reading.controller.admin.banner;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.reading.controller.admin.banner.vo.*;
import cn.iocoder.yudao.module.reading.convert.banner.BannerConvert;
import cn.iocoder.yudao.module.reading.dal.dataobject.banner.BannerDO;
import cn.iocoder.yudao.module.reading.service.banner.BannerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 横幅管理
 *
 * @author 李晨
 */
@Tag(name = "管理后台 - 横幅管理")
@RestController("adminBannerController")
@RequestMapping("/reading/admin/banners")
@Validated
@Slf4j
public class AdminBannerController {

    @Resource
    private BannerService bannerService;

    @PostMapping("/create")
    @Operation(summary = "创建横幅")
    public CommonResult<Long> createBanner(@Valid @RequestBody BannerCreateReqVO createReqVO) {
        Long bannerId = bannerService.createBanner(
                createReqVO.getTitle(),
                createReqVO.getDescription(),
                createReqVO.getImageUrl(),
                createReqVO.getLinkUrl(),
                createReqVO.getLinkType(),
                createReqVO.getSortOrder(),
                createReqVO.getStatus(),
                createReqVO.getStartTime(),
                createReqVO.getEndTime()
        );
        return success(bannerId);
    }

    @PutMapping("/update")
    @Operation(summary = "更新横幅")
    public CommonResult<Boolean> updateBanner(@Valid @RequestBody BannerUpdateReqVO updateReqVO) {
        bannerService.updateBanner(
                updateReqVO.getId(),
                updateReqVO.getTitle(),
                updateReqVO.getDescription(),
                updateReqVO.getImageUrl(),
                updateReqVO.getLinkUrl(),
                updateReqVO.getLinkType(),
                updateReqVO.getSortOrder(),
                updateReqVO.getStatus(),
                updateReqVO.getStartTime(),
                updateReqVO.getEndTime()
        );
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除横幅")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteBanner(@RequestParam("id") Long id) {
        bannerService.deleteBanner(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得横幅")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BannerRespVO> getBanner(@RequestParam("id") Long id) {
        BannerDO banner = bannerService.getBanner(id);
        return success(BannerConvert.INSTANCE.convertAdmin(banner));
    }

    @GetMapping("/page")
    @Operation(summary = "获得横幅分页")
    public CommonResult<PageResult<BannerRespVO>> getBannerPage(@Valid BannerPageReqVO pageReqVO) {
        PageResult<BannerDO> pageResult = bannerService.getBannerPage(pageReqVO);
        return success(BannerConvert.INSTANCE.convertAdminPage(pageResult));
    }

    @PutMapping("/update-status-batch")
    @Operation(summary = "批量更新横幅状态")
    public CommonResult<Boolean> updateBannerStatusBatch(@Valid @RequestBody BannerUpdateStatusBatchReqVO reqVO) {
        bannerService.updateBannerStatusBatch(reqVO.getIds(), reqVO.getStatus());
        return success(true);
    }

    @DeleteMapping("/delete-batch")
    @Operation(summary = "批量删除横幅")
    public CommonResult<Boolean> deleteBannerBatch(@RequestBody List<Long> ids) {
        bannerService.deleteBannerBatch(ids);
        return success(true);
    }

}
